package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 查询所有菜单信息
     * @return
     */
    @GetMapping("/findAllMenu")
    public Result findAllMenu(){
        try {
            List<Menu> list = menuService.findAllRole();
            return new Result(true, MessageConstant.QUERY_Role_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_Role_FAIL);
        }
    }


    /**
     * 分页查询用户数据
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        try {
            PageResult pageResult = menuService.findPage(queryPageBean);
            //取出查询结果
            List<Menu> list = pageResult.getRows();
            //遍历判断
            list.stream().forEach((menu)->{
                //跟据level判断是否父菜单
                if(menu.getLevel() == 1){
                    menu.setHasChildren(true);
                }
            });
            System.out.println(list);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    /**
     * 新增菜单信息
     * @param menu
     * @return
     */
    @PostMapping("/saveMenu")
   public Result saveMenu(@RequestBody Menu menu){
        try {
            menuService.saveMenu(menu);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
   }

    /**
     * 根据id删除菜单
     * @param id
     * @return
     */
    @DeleteMapping("/deleteMenu/{id}")
    public Result deleteMenu(@PathVariable Integer id){

        try {
            //删除菜单
            Result result = menuService.deleteMenu(id);
            return result;
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL );
        }
    }

    /**
     * 更新菜单信息
     * @param menu
     * @return
     */
    @PutMapping("/updateMenu")
    public Result updateMenu(@RequestBody Menu menu){
        try {
            menuService.updateMenu(menu);
            return new Result(false,MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }

    /**
     * 查询所有父级菜单
     * @return
     */
    @GetMapping("/findAllParents")
    public Result findAllParents(){
        try {
            List<Menu> menuList = menuService.findAllParents();
            return new Result(true,MessageConstant.QUERY_MENUPARENT_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENUPARENT_FAIL);
        }
    }

    /**
     * 加载二级菜单
     * @return
     */
    @GetMapping("/findChildrensMenuById/{id}")
    public Result findChildrensMenuById(@PathVariable Integer id){
        try {
            List<Menu>  menuChildrensList = menuService.findChildrensMenuById(id);
            return new Result(true,MessageConstant.QUERY_CHILDRENMENU_SUCCESS,menuChildrensList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHILDRENMENU_FAIL);
        }
    }
}
